#include <afxdisp.h>
#include <atlbase.h>
#include <msxml.h>
#include <iostream>
#include <string>
#include <comdef.h>
#include <vector>
#include "Graph.h"
using namespace std;

class CSerialization
{
	CSerialization();
	static void serializeVertex(vector<CNode<CGtsNode>*>* vNodes, CComPtr<IXMLDOMDocument> spXMLDOM);
	static void serializeEdge(vector<CEdge<CGtsNode, CPipe>*>* vEdges, CComPtr<IXMLDOMDocument> spXMLDOM);
	static void setAttr(CComQIPtr<IXMLDOMElement> el, string tag, string value);
	static void setAttr(CComQIPtr<IXMLDOMElement> el, string tag, int value);
	static void setAttr(CComQIPtr<IXMLDOMElement> el, string tag, double value);
	static vector<CNode<CGtsNode>*>* returnVNodes(CComPtr<IXMLDOMNode> parentNode);

	static vector<CEdge<CGtsNode, CPipe>*>* returnVEdges(CComPtr<IXMLDOMNode> parentNode, vector<CNode<CGtsNode>*>* vNode);

	static CGtsNode * returnGtsNode(int type, double iPower,string name, double iInFlow, double iOutFlow,int iAmountIn,
		int iAmountOut, int id, double x,double y, double * out);
	static CNode<CGtsNode>* returnNodeById(int id, vector<CNode<CGtsNode>*>* vNode);
public:
static void doSerilaze(CGraph<CGtsNode,CPipe>* graph,const string& fileName);
static CGraph<CGtsNode,CPipe>* doDeSerilaze(const string& fileName);

};

void CSerialization::setAttr(CComQIPtr<IXMLDOMElement> el, string tag, string value)
{
	//try?
	COleVariant objVariant(value.c_str());
	VARIANT var_val = objVariant.Detach();
	//BSTR b = ((char *)tag).c_str();
	HRESULT hr = el->setAttribute(CComBSTR(tag.c_str()),var_val);

}

void CSerialization::setAttr(CComQIPtr<IXMLDOMElement> el, string tag, int value)
{

	HRESULT hr = el->setAttribute(CComBSTR(tag.c_str()),CComVariant(value));

}
void CSerialization::setAttr(CComQIPtr<IXMLDOMElement> el, string tag, double value)
{

	HRESULT hr = el->setAttribute(CComBSTR(tag.c_str()),CComVariant(value));

}


void CSerialization::doSerilaze(CGraph<CGtsNode,CPipe>* graph,const string& fileName)
{

	COleVariant objVariant( fileName.c_str() );
	VARIANT variant = objVariant.Detach();

	vector<CNode<CGtsNode>*>* vNodes=graph->getNodeList();
	vector<CEdge<CGtsNode, CPipe>*>* vEdges = graph->getEdgeList();
	CoInitialize(NULL);
	


	try {
		// Create an instance of the parser
			CComPtr<IXMLDOMDocument> spXMLDOM;
			HRESULT hr = spXMLDOM.CoCreateInstance(__uuidof(DOMDocument));
			if ( FAILED(hr) ) throw "Unable to create XML parser object";
			if ( spXMLDOM.p == NULL ) throw "Unable to create XML parser object";

		// Load the XML document file...
			VARIANT_BOOL bSuccess = false;
			hr = spXMLDOM->load(variant,&bSuccess);
			if ( FAILED(hr) ) throw "Unable to load XML document into the parser";
			if ( !bSuccess ) throw "Unable to load XML document into the parser";

			serializeVertex(vNodes, spXMLDOM);
			serializeEdge(vEdges, spXMLDOM);

			hr = spXMLDOM->save(CComVariant("updatedxml.xml"));
			if ( FAILED(hr) ) throw "Unable to save updated XML document";
	}
	catch(char* lpstrErr) {
		AfxMessageBox(lpstrErr);
	}
	/*	catch(...) {
		AfxMessageBox("Uknown error");
	} */
	CoUninitialize();
}

CGraph<CGtsNode,CPipe> * CSerialization::doDeSerilaze(const string& fileName)
{
	
	CGraph<CGtsNode,CPipe> * grs;

	
	CoInitialize(NULL);
	COleVariant objVariant( fileName.c_str() );
	VARIANT variant = objVariant.Detach();


	try {
		// Create an instance of the parser
		CComPtr<IXMLDOMDocument> spXMLDOM;
		HRESULT hr = spXMLDOM.CoCreateInstance(__uuidof(DOMDocument));
		if ( FAILED(hr) ) throw "Unable to create XML parser object";
		if ( spXMLDOM.p == NULL ) throw "Unable to create XML parser object";

		// Load the XML document file...
		VARIANT_BOOL bSuccess = false;
		hr = spXMLDOM->load(CComVariant(variant),&bSuccess);
		if ( FAILED(hr) ) throw "Unable to load XML document into the parser";
		if ( !bSuccess ) throw "Unable to load XML document into the parser";

		// Give the first node (parent for all)

		CComBSTR bstrSS(L"xmldata");
		CComPtr<IXMLDOMNode> parentNode;
		hr = spXMLDOM->selectSingleNode(bstrSS,&parentNode);
		if ( FAILED(hr) ) throw "Unable to locate 'xmlnode' XML node";
		if ( parentNode.p == NULL ) throw "Unable to locate 'xmlnode' XML node";
		
		grs = new CGraph<CGtsNode,CPipe>(returnVNodes(parentNode),returnVEdges(parentNode, returnVNodes(parentNode)));

	
		


	} // try
	catch(char* lpstrErr) {
		// Some error...
		std::cout << lpstrErr << std::endl << std::endl;
	} // catch
	catch(...) {
		// Unknown error...
		std::cout << "Unknown error..." << std::endl << std::endl;
	} // catch

	// Stop COM
	CoUninitialize();


	return grs;		
}

void CSerialization::serializeVertex(vector<CNode<CGtsNode>*>* vNodes, CComPtr<IXMLDOMDocument> spXMLDOM)
{
	CComBSTR bstrSS(L"xmldata");
	CComPtr<IXMLDOMNode> parentNode;
	CComPtr<IXMLDOMNode> childNode;
	CComPtr<IXMLDOMNode> spInsertedNode;
	CComQIPtr<IXMLDOMElement> spXMLChildElement;
	CComQIPtr<IXMLDOMElement> sEl;
	CComPtr<IXMLDOMNode> outNode;
	CComPtr<IXMLDOMNode> InsOut;
	string type, name;
	try {
			//main node (childNode)

			HRESULT hr = spXMLDOM->selectSingleNode(bstrSS,&parentNode);
			if ( FAILED(hr) ) throw "Unable to locate 'xmlnode' XML node";
			if ( parentNode.p == NULL ) throw "Unable to locate 'xmlnode' XML node";

			for (int i=0;i<vNodes->size();i++)
			{	
				//create new node
				childNode=NULL;
				hr = spXMLDOM->createNode(CComVariant(NODE_ELEMENT),CComBSTR(L"vertex"),NULL,&childNode);
				if ( FAILED(hr) ) throw "Unable to create 'xmlchildnode' XML node";
				if ( childNode.p == NULL ) throw "Unable to create 'xmlchildnode' XML node";
				//insert new node
				spInsertedNode=NULL;
				hr = parentNode->appendChild(childNode,&spInsertedNode);
				if ( FAILED(hr) ) throw "Unable to move 'xmlchildnode' XML node";
				if ( spInsertedNode.p == NULL ) throw "Unable to move 'xmlchildnode' XML node";

				// Add the attributes. 
				
				spXMLChildElement = spInsertedNode;
				if ( spXMLChildElement.p == NULL ) throw "Unable to query for 'xmlchildnode' XML element interface";

				setAttr(spXMLChildElement,"type",vNodes->at(i)->getElement()->getType());
				setAttr(spXMLChildElement,"name",vNodes->at(i)->getElement()->getName());
				setAttr(spXMLChildElement,"id",vNodes->at(i)->getElement()->getId());
				setAttr(spXMLChildElement,"iPower",vNodes->at(i)->getElement()->getPower());
				setAttr(spXMLChildElement,"iInFlow",vNodes->at(i)->getElement()->getInFlow());
				setAttr(spXMLChildElement,"iOutFlow",vNodes->at(i)->getElement()->getOutFlow());
				setAttr(spXMLChildElement,"iAmountIn",vNodes->at(i)->getElement()->getInAmount());
				setAttr(spXMLChildElement,"iAmountOut",vNodes->at(i)->getElement()->getOutAmount());
				setAttr(spXMLChildElement,"positionX",vNodes->at(i)->getElement()->getPosition()->x);
				setAttr(spXMLChildElement,"positionY",vNodes->at(i)->getElement()->getPosition()->y);
				setAttr(spXMLChildElement,"state",vNodes->at(i)->getElement()->getState());
			/*	int t=vNodes->size();
				setAttr(spXMLChildElement,"debug_size",t);*/
				
				for (int j=0; j<vNodes->at(i)->getElement()->getOutAmount();j++)
				{
					//create new node
					outNode=NULL;
					hr = spXMLDOM->createNode(CComVariant(NODE_ELEMENT),CComBSTR(L"out"),NULL,&outNode);
					if ( FAILED(hr) ) throw "Unable to create 'xmlchildnode' XML node";
					if ( childNode.p == NULL ) throw "Unable to create 'xmlchildnode' XML node";

					InsOut=NULL;
					hr = childNode->appendChild(outNode,&InsOut);
					if ( FAILED(hr) ) throw "Unable to move 'xmlchildnode' XML node";
					if ( spInsertedNode.p == NULL ) throw "Unable to move 'xmlchildnode' XML node";

					sEl = InsOut;
					if ( spXMLChildElement.p == NULL ) throw "Unable to query for 'xmlchildnode' XML element interface";
					setAttr(sEl,"val",vNodes->at(i)->getElement()->getOut()[j]);
					//hr=outNode->put_nodeValue(CComVariant(vNodes->at(i)->getElement()->getOut()[j]));
					//hr=InsOut->put_text(CComBSTR(L"text"));//put_nodeValue(CComVariant(L"vhjcfcfdrxrx"));
					if ( FAILED(hr) ) throw "Unable to put value in XML node";
					if ( spInsertedNode.p == NULL ) throw "Unable to put value";


				}



			}
	}
		catch(char* lpstrErr) {
		AfxMessageBox(lpstrErr);
	}


}



void CSerialization::serializeEdge(vector<CEdge<CGtsNode, CPipe>*>* vEdges, CComPtr<IXMLDOMDocument> spXMLDOM)
{
	CComBSTR bstrSS(L"xmldata");
	CComPtr<IXMLDOMNode> parentNode;
	CComPtr<IXMLDOMNode> childNode;
	CComPtr<IXMLDOMNode> spInsertedNode;
	CComQIPtr<IXMLDOMElement> spXMLChildElement;
	CComQIPtr<IXMLDOMElement> sEl;
	CComPtr<IXMLDOMNode> outNode;
	CComPtr<IXMLDOMNode> InsOut;

	string type, name;
	try {
			//main node (childNode)

			HRESULT hr = spXMLDOM->selectSingleNode(bstrSS,&parentNode);
			if ( FAILED(hr) ) throw "Unable to locate 'xmlnode' XML node";
			if ( parentNode.p == NULL ) throw "Unable to locate 'xmlnode' XML node";

			for (int i=0;i<vEdges->size();i++)
			{	
				//create new node
				childNode=NULL;
				hr = spXMLDOM->createNode(CComVariant(NODE_ELEMENT),CComBSTR(L"edge"),NULL,&childNode);
				if ( FAILED(hr) ) throw "Unable to create 'xmlchildnode' XML node";
				if ( childNode.p == NULL ) throw "Unable to create 'xmlchildnode' XML node";
				//insert new node
				spInsertedNode=NULL;
				hr = parentNode->appendChild(childNode,&spInsertedNode);
				if ( FAILED(hr) ) throw "Unable to move 'xmlchildnode' XML node";
				if ( spInsertedNode.p == NULL ) throw "Unable to move 'xmlchildnode' XML node";

				// Add the attributes. 
				
				spXMLChildElement = spInsertedNode;
				if ( spXMLChildElement.p == NULL ) throw "Unable to query for 'xmlchildnode' XML element interface";

				setAttr(spXMLChildElement,"idSource",vEdges->at(i)->getSource()->getElement()->getId());
				setAttr(spXMLChildElement,"idTarget",vEdges->at(i)->getTarget()->getElement()->getId());
				setAttr(spXMLChildElement,"iWorkFlow",vEdges->at(i)->getElement()->getWorkFlow());
				setAttr(spXMLChildElement,"iCurrentFlow",vEdges->at(i)->getElement()->getCurrentFlow());
				setAttr(spXMLChildElement,"iSourceConnectorIndex",vEdges->at(i)->getElement()->getSourceConnectorIndex());
				setAttr(spXMLChildElement,"iTargetConnectorIndex",vEdges->at(i)->getElement()->getTargetConnectorIndex());

				
				for (int j=0; j<vEdges->at(i)->getElement()->getPoints()->size();j++)
				{
					//create new node
					outNode=NULL;
					hr = spXMLDOM->createNode(CComVariant(NODE_ELEMENT),CComBSTR(L"point"),NULL,&outNode);
					if ( FAILED(hr) ) throw "Unable to create 'xmlchildnode' XML node";
					if ( childNode.p == NULL ) throw "Unable to create 'xmlchildnode' XML node";

					InsOut=NULL;
					hr = childNode->appendChild(outNode,&InsOut);
					if ( FAILED(hr) ) throw "Unable to move 'xmlchildnode' XML node";
					if ( spInsertedNode.p == NULL ) throw "Unable to move 'xmlchildnode' XML node";

					sEl = InsOut;
					if ( spXMLChildElement.p == NULL ) throw "Unable to query for 'xmlchildnode' XML element interface";
					setAttr(sEl,"positionX",vEdges->at(i)->getElement()->getPoints()->at(j).x);
					setAttr(sEl,"positionY",vEdges->at(i)->getElement()->getPoints()->at(j).y);
					//hr=outNode->put_nodeValue(CComVariant(vNodes->at(i)->getElement()->getOut()[j]));
					//hr=InsOut->put_text(CComBSTR(L"text"));//put_nodeValue(CComVariant(L"vhjcfcfdrxrx"));
					if ( FAILED(hr) ) throw "Unable to put value in XML node";
					if ( spInsertedNode.p == NULL ) throw "Unable to put value";


				}



			}
	}
		catch(char* lpstrErr) {
		AfxMessageBox(lpstrErr);
	}


}


vector<CNode<CGtsNode>*>* CSerialization::returnVNodes(CComPtr<IXMLDOMNode> parentNode)
{
	int type;
	double iPower;	
	double iInFlow;
	double iOutFlow;
	int iAmountIn;
	int iAmountOut;
	int id;
	double x,y;
	double * out;
	int state;
	CComPtr<IXMLDOMNodeList>childList; 
	CComPtr<IXMLDOMNode> listItem;
	CComPtr<IXMLDOMNamedNodeMap> attributeMap;
	CComPtr<IXMLDOMNode> attrNode;
	CComPtr<IXMLDOMNodeList>outList;
	CComPtr<IXMLDOMNode> outNode;
	CComPtr<IXMLDOMNamedNodeMap> outAttr;
	CComPtr<IXMLDOMNode> outAtrrNode;
	long la=0;
	BSTR txt;
	vector<CNode<CGtsNode>*>* vNodes = new vector<CNode<CGtsNode>*>();
	string name;
	
	
	//childList=NULL;
	long listLength = 0;
	try {

		HRESULT hr=parentNode->selectNodes(CComBSTR(L"vertex"),&childList); 
		if ( FAILED(hr) ) throw "Unable childList";
		if ( childList.p == NULL ) throw "Unable childList";

		hr=childList->get_length(&listLength);
		if ( FAILED(hr) ) throw "Unable length";
	
		
		for (int i=0;i<(listLength);i++)
		{
			listItem=NULL;
			hr=childList->get_item(i,&listItem);
			if ( FAILED(hr) ) throw "Unable type";
			if ( listItem.p == NULL ) throw "Unable type";
			attributeMap=NULL;
			hr=listItem->get_attributes(&attributeMap);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attributeMap.p == NULL ) throw "Unable type";
			
			attrNode=NULL;
			hr=attributeMap->get_item(0,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			type = _wtoi(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(1,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			name =_bstr_t(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(2,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			id = _wtoi(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(3,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iPower = _wtof(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(4,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iInFlow = _wtof(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(5,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iOutFlow = _wtof(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(6,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iAmountIn = _wtoi(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(7,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iAmountOut = _wtoi(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(8,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			x = _wtoi(txt);

			attrNode=NULL;
			hr=attributeMap->get_item(9,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			y = _wtoi(txt);


			VARIANT_BOOL* var=false;
			hr=listItem->hasChildNodes(var);
			if (&var)
			{
				outList=NULL;
				hr=listItem->get_childNodes(&outList);	 
				if ( FAILED(hr) ) throw "Unable childList";
				if ( outList.p == NULL ) throw "Unable childList";
				la=0;
				hr=outList->get_length(&la);
				if ( FAILED(hr) ) throw "Unable childList";
				out = new double[la];
				for (int j=0; j<la; j++)
				{
					outNode=NULL;
					hr=outList->get_item(j,&outNode);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outNode.p == NULL ) throw "Unable type";
					outAttr=NULL;
					hr=outNode->get_attributes(&outAttr);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outAttr.p == NULL ) throw "Unable type";
					outAtrrNode=NULL;
					hr=attributeMap->get_item(0,&outAtrrNode);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outAtrrNode.p == NULL ) throw "Unable type";
					outAtrrNode->get_text(&txt);
					out[j]=_wtof(txt);
				}


	
			}
			CNode<CGtsNode>* node = new CNode<CGtsNode>(returnGtsNode(type,iPower,name,iInFlow,iOutFlow,iAmountIn,iAmountOut,id,x,y,out));
			vNodes->push_back(node);
		}
		}
		catch(char* lpstrErr) {
		AfxMessageBox(lpstrErr);
		
	}
		return vNodes;
		}




	CGtsNode *CSerialization::returnGtsNode(int type, double iPower, string name, double iInFlow, double iOutFlow,int iAmountIn,
		int iAmountOut, int id, double x,double y, double * out)
	{
		CGtsNode *gtsNode;
		switch (type) {
		case 0: gtsNode = new CSource(iPower, iAmountOut, out);
			break;
		case 1: gtsNode = new CTarget(iPower);
			break;
		case 2: gtsNode = new CGrs(iAmountIn,iAmountOut,out);
		}
			gtsNode->setName(name);
			gtsNode->setId(id);
			gtsNode->setInFlow(iInFlow);
			gtsNode->setOutFlow(iOutFlow);
			gtsNode->setPosition(new CPoint(x,y));
			
			return gtsNode;

	}


	vector<CEdge<CGtsNode, CPipe>*>* CSerialization::returnVEdges(CComPtr<IXMLDOMNode> parentNode, vector<CNode<CGtsNode>*>* vNode)
	{
		vector<CEdge<CGtsNode, CPipe>*>* vEdges = new vector<CEdge<CGtsNode, CPipe>*>();
		vector<CPoint>* vPoints;
		int idSource;
		int idTarget;
		double iWorkFlow;
		double iCurrentFlow;
		int iSourceConnectorIndex;
		int iTargetConnectorIndex;
		CComPtr<IXMLDOMNodeList>childList; 
		CComPtr<IXMLDOMNode> listItem;
		CComPtr<IXMLDOMNamedNodeMap> attributeMap;
		CComPtr<IXMLDOMNode> attrNode;
		CComPtr<IXMLDOMNodeList>outList;
		CComPtr<IXMLDOMNode> outNode;
		CComPtr<IXMLDOMNamedNodeMap> outAttr;
		CComPtr<IXMLDOMNode> outAtrrNode;
		long la=0;
		BSTR txt;
		vector<CNode<CGtsNode>*>* vNodes = new vector<CNode<CGtsNode>*>();
		double x,y;
		CPipe * pipe;
		
	
	
	//childList=NULL;
	long listLength = 0;
	try {

		HRESULT hr=parentNode->selectNodes(CComBSTR(L"edge"),&childList); 
		if ( FAILED(hr) ) throw "Unable childList";
		if ( childList.p == NULL ) throw "Unable childList";

		hr=childList->get_length(&listLength);
		if ( FAILED(hr) ) throw "Unable length";
	
		
		for (int i=0;i<(listLength);i++)
		{
			listItem=NULL;
			hr=childList->get_item(i,&listItem);
			if ( FAILED(hr) ) throw "Unable type";
			if ( listItem.p == NULL ) throw "Unable type";
			attributeMap=NULL;
			hr=listItem->get_attributes(&attributeMap);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attributeMap.p == NULL ) throw "Unable type";
			attrNode=NULL;
			hr=attributeMap->get_item(0,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			idSource = _wtoi(txt);
			attrNode=NULL;
			hr=attributeMap->get_item(1,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			idTarget = _wtoi(txt);
			attrNode=NULL;
			hr=attributeMap->get_item(2,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iWorkFlow = _wtof(txt);
			attrNode=NULL;
			hr=attributeMap->get_item(3,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iCurrentFlow = _wtof(txt);
			attrNode=NULL;
			hr=attributeMap->get_item(4,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iSourceConnectorIndex = _wtoi(txt);
			attrNode=NULL;
			hr=attributeMap->get_item(5,&attrNode);
			if ( FAILED(hr) ) throw "Unable type";
			if ( attrNode.p == NULL ) throw "Unable type";
			attrNode->get_text(&txt);
			iTargetConnectorIndex = _wtoi(txt);

			outList=NULL;
			hr=listItem->get_childNodes(&outList);	 
			if ( FAILED(hr) ) throw "Unable childList";
			if ( outList.p == NULL ) throw "Unable childList";
			la=0;		
			hr=outList->get_length(&la);
			if ( FAILED(hr) ) throw "Unable childList";
			vPoints=new vector<CPoint>();
				for (int j=0; j<la; j++)
				{
					outNode=NULL;
					hr=outList->get_item(j,&outNode);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outNode.p == NULL ) throw "Unable type";
					outAttr=NULL;
					hr=outNode->get_attributes(&outAttr);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outAttr.p == NULL ) throw "Unable type";
					outAtrrNode=NULL;
					hr=attributeMap->get_item(0,&outAtrrNode);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outAtrrNode.p == NULL ) throw "Unable type";
					outAtrrNode->get_text(&txt);
					x=_wtof(txt);
					outAttr=NULL;
					hr=outNode->get_attributes(&outAttr);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outAttr.p == NULL ) throw "Unable type";
					outAtrrNode=NULL;
					hr=attributeMap->get_item(1,&outAtrrNode);
					if ( FAILED(hr) ) throw "Unable type";
					if ( outAtrrNode.p == NULL ) throw "Unable type";
					outAtrrNode->get_text(&txt);
					y=_wtof(txt);
					vPoints->push_back(CPoint(x,y));
				}
				pipe=new CPipe(iWorkFlow);
				pipe->setCurrentFlow(iCurrentFlow);
				pipe->setSourceConnectorIndex(iSourceConnectorIndex);
				pipe->setTargetConnectorIndex(iTargetConnectorIndex);
				pipe->setPoints(vPoints);

				CNode<CGtsNode>* s = returnNodeById(idSource,vNode);//new CNode<CGtsNode>(returnNodeById(idSource,vNode));
				CNode<CGtsNode>* t = returnNodeById(idTarget,vNode);//new CNode<CGtsNode>(returnNodeById(idTarget,vNode));
				CEdge<CGtsNode, CPipe>* edge = new CEdge<CGtsNode, CPipe>(pipe,s,t);
				

				vEdges->push_back(edge);


		}
	}

			catch(char* lpstrErr) {
		AfxMessageBox(lpstrErr);
		
	}
			return vEdges;
	}


	CNode<CGtsNode> *CSerialization::returnNodeById(int id, vector<CNode<CGtsNode>*>* vNode)
	{
		//CGtsNode *node=new CGtsNode();
		CNode<CGtsNode>* node = NULL;
		int i=0;
		while ((vNode->at(i)->getElement()->getId()!=id)&&(i<vNode->size()))
		{
			i++;
			node=vNode->at(i);//->getElement();

		}
		return node;
	}